package com.tsll.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.tsll.dao.UserMapper;
import com.tsll.entity.User;
import com.tsll.service.UserService;
import com.tsll.vo.Result;
import com.tsll.vo.StaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Result selectAll(Integer current, Integer pageSize, StaffVo staffVo) {
        PageHelper.startPage(current,pageSize);
        List<User> users = userMapper.selectAlls(staffVo);
        PageInfo<User> pageInfo=new PageInfo<>(users);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public int update(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        return i;
    }

    @Override
    public int delete(Integer id) {
        int i = userMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public Result insertAll(User user) {
        int insert = userMapper.insert(user);
        return new Result(200,"添加成功",null);
    }
//获取用户数户数量
    @Override
    public Result getusershu() {
        return new Result(200,"查询用户数量成功",userMapper.getusershu());
    }

    @Override
    public Result selectByCount(String no) {
        User user = userMapper.selectByCount(no);
        System.out.println(user);
        if(user!=null){
            return new Result(500,"用户账号重复请重新输入",null);
        }
        return new Result(200,"",null);

    }

    @Override
    public Result login(User user) {
        // 检查用户名和密码是否为空
        if (StringUtil.isEmpty(user.getUserAccount()) || StringUtil.isEmpty(user.getUserPwd())) {
            return new Result(500, "账号密码为空", null);
        }
        // 调用staffService进行用户名和密码的验证
        User user1 = userMapper.isEmpty(user.getUserAccount(), user.getUserPwd());
        System.out.println("============user1==================");
        System.out.println(user1);

        if(user1==null){

           return new Result(200,"账号或密码不正确",user1);
        }

        return new Result(200,"登录成功",user1);
    }

}
